package com.zwz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zwz.bean.SysResourceBean;
import com.zwz.model.SysResource;
import com.zwz.service.baseConfig.SysResourceService;
import com.zwz.utils.FunctionUtils;

/**
 * 资源
 * @author zhangwanzhong
 * 2015年12月29日 下午1:40:14
 */
@Controller
@RequestMapping("/resource")
public class SysResourceController extends BaseController {
	@Autowired
	private SysResourceService sysResourceService;
	
	@RequestMapping(value="getView")
	public String getView(HttpServletRequest request){
		request.setAttribute("roleAuth", getRoleAuth());
		return "resource";
	}
	
	@RequestMapping(value="/queryByList")
	public void getTree(SysResourceBean model,HttpServletRequest request,HttpServletResponse response)throws Exception{
		SysResourceBean sysResource=new SysResourceBean();
		sysResource.setResource_type("0");
		sysResource.setRole_id("0");
		sysResource.setTag("0");
		List<SysResource> sysResourceList = sysResourceService.findAllResourceByRole(sysResource);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",model.getPager().getRowCount());
		jsonMap.put("rows", sysResourceList);
		String json=JSON.toJSONString(jsonMap);
		System.out.println("资源:"+json);
		response.getWriter().write(json);
	}
	
	/**
	 * 查询资源列表
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/queryResourceByList")
	@ResponseBody
	public Object getSysResource(SysResourceBean model,HttpServletRequest request,HttpServletResponse response){
		List<SysResource> dataList=sysResourceService.findAllPage(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",model.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		return jsonMap;
	}
	
	/**
	 * 查询菜单树形结构
	 * @return
	 */
	@RequestMapping(value="/getTreeMenu")
	@ResponseBody
	public Object getTreeMenu(){
		List<Map<String, Object>> list=sysResourceService.findAllPid();
		return list;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(SysResource resource,HttpServletRequest request){
		resource.setId(FunctionUtils.getUUID());
		resource.setIcon(request.getParameter("iconCls"));
		sysResourceService.add(resource);
		return sendSuccessMessage("处理成功");
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(SysResource resource,HttpServletRequest request){
		resource.setIcon(request.getParameter("iconCls"));
		sysResourceService.update(resource);
		return sendSuccessMessage("处理成功");
	}
	
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(String id){
		sysResourceService.delete(id);
		return sendSuccessMessage("删除成功");
	}
	/**
	 * 查询全部资源树(三级)
	 * @return
	 */
	@RequestMapping(value="/findAllResourceTree")
	@ResponseBody
	public Object findAllResourceTree(){
		return sysResourceService.findAllTrees();
	}
	
	/**
	 *查询角色资源ID 
	 * @return
	 */
	@RequestMapping(value="/findRoleResourceListId")
	@ResponseBody
	public Object findRoleResourceListId(String roleId){
		return sysResourceService.findRoleResourceId(roleId);
	}
	
	/**
	 * 保存角色资源权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/saveAuth")
	@ResponseBody
	public Object saveAuth(HttpServletRequest request)throws Exception{
		String[] ids=request.getParameterValues("resourceIds[]");
		String roleId=request.getParameter("roleId");
		sysResourceService.saveAuth(ids, roleId);
		return sendSuccessMessage("处理成功");
	}
}
